//
//  UFOSXPPicPageCollectionViewCell.m
//  unrivalledForUniverse
//
//  Created by dllo on 16/1/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "UFOSXPPicPageCollectionViewCell.h"
#import "UFOPicPageListCollectionViewCell.h"
#import "UFOSXPPicPageListModel.h"

#define SPACE 10
@interface UFOSXPPicPageCollectionViewCell () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, retain) UICollectionView *picCollectionV;

@end
@implementation UFOSXPPicPageCollectionViewCell

- (void)dealloc
{
    [_picListArr release];
    [_picCollectionV release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}

// 重写setter方法
- (void)setPicListArr:(NSMutableArray *)picListArr
{
    if (_picListArr != picListArr) {
        [_picListArr release];
        _picListArr = [picListArr retain];
    }
    [self.picCollectionV reloadData];
    [self.picCollectionV.mj_footer endRefreshing];
}

// 创建视图
- (void)createSubviews
{
    self.backgroundColor = [UIColor redColor];
    [self createPicCollectionView];
}

// 创建collectionView
- (void)createPicCollectionView
{
    UICollectionViewFlowLayout *flowL = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    flowL.minimumLineSpacing = SPACE * SCREENHEIGHT / HEIGHTSIXP;
    flowL.minimumInteritemSpacing = SPACE * SCREENWIDTH / WIDTHSIXP;
    flowL.headerReferenceSize = CGSizeMake(0, 0);
    flowL.footerReferenceSize = CGSizeMake(0, 0);
    
    flowL.itemSize = CGSizeMake((SCREENWIDTH - 4 * SPACE * SCREENWIDTH / WIDTHSIXP) / 3, (SCREENWIDTH - 4 * SPACE * SCREENWIDTH / WIDTHSIXP) / 3 * 1.6);
    flowL.sectionInset = UIEdgeInsetsMake(SPACE * SCREENHEIGHT / HEIGHTSIXP, SPACE * SCREENWIDTH / WIDTHSIXP, SPACE * SCREENHEIGHT / HEIGHTSIXP, SPACE * SCREENWIDTH / WIDTHSIXP);
    flowL.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.picCollectionV = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowL];
    self.picCollectionV.backgroundColor = [UIColor whiteColor];
    self.picCollectionV.delegate = self;
    self.picCollectionV.dataSource = self;
    self.picCollectionV.bounces = YES;
    
    [self.contentView addSubview:self.picCollectionV];
    [_picCollectionV release];
    
    [self.picCollectionV registerClass:[UFOPicPageListCollectionViewCell class] forCellWithReuseIdentifier:@"picListCell"];
    
    // 上拉加载
    self.picCollectionV.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    
    [self.picCollectionV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.left.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView);
    }];
}

// 单元数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.picListArr) {
        return self.picListArr.count;
    }
    return 0;
}

// 自定义cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UFOPicPageListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"picListCell" forIndexPath:indexPath];
    if (self.picListArr) {
        UFOSXPPicPageListModel *model = [self.picListArr objectAtIndex:indexPath.item];
        [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.diy] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    }
    return cell;
}

// 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate enterPicDetailPageWithIndex:indexPath.item];
}

// 上拉重载
- (void)footerRefresh
{
    [self.delegate getMorePicPageListData];
}

@end
